const express = require("express");
const router = express.Router();
const staffInfoDao = require("../database/StaffInfoDao");

// 获取指定用户的考勤记录
router.get("/:userId", async (req, res) => {
    try {
        const userId = req.params.userId;
        const records = await staffInfoDao.getAttendanceByUserId(userId);
        res.send({
            code: 2000,
            msg: "获取考勤记录成功",
            data: records
        });
    } catch (error) {
        console.error("获取考勤记录失败:", error);
        res.send({
            code: 5000,
            msg: "获取考勤记录失败",
            data: null
        });
    }
});

// 添加考勤记录
router.post("/:userId", async (req, res) => {
    try {
        const userId = req.params.userId;
        const { atd_absentdays, atd_money, atd_date } = req.body;
        
        if (!atd_absentdays || !atd_money || !atd_date) {
            return res.send({
                code: 4000,
                msg: "参数不完整",
                data: null
            });
        }

        const id = await staffInfoDao.addAttendance({
            user_id: userId,
            atd_absentdays,
            atd_money,
            atd_date
        });

        res.send({
            code: 2000,
            msg: "添加考勤记录成功",
            data: { id }
        });
    } catch (error) {
        console.error("添加考勤记录失败:", error);
        res.send({
            code: 5000,
            msg: "添加考勤记录失败",
            data: null
        });
    }
});

// 更新考勤记录
router.put("/:id", async (req, res) => {
    try {
        const id = req.params.id;
        const { atd_absentdays, atd_money, atd_date } = req.body;
        
        if (!atd_absentdays || !atd_money || !atd_date) {
            return res.send({
                code: 4000,
                msg: "参数不完整",
                data: null
            });
        }

        const success = await staffInfoDao.updateAttendance(id, {
            atd_absentdays,
            atd_money,
            atd_date
        });

        if (success) {
            res.send({
                code: 2000,
                msg: "更新考勤记录成功",
                data: null
            });
        } else {
            res.send({
                code: 4000,
                msg: "更新考勤记录失败，记录不存在",
                data: null
            });
        }
    } catch (error) {
        console.error("更新考勤记录失败:", error);
        res.send({
            code: 5000,
            msg: "更新考勤记录失败",
            data: null
        });
    }
});

// 删除考勤记录
router.delete("/:id", async (req, res) => {
    try {
        const id = req.params.id;
        const success = await staffInfoDao.deleteAttendance(id);
        
        if (success) {
            res.send({
                code: 2000,
                msg: "删除考勤记录成功",
                data: null
            });
        } else {
            res.send({
                code: 4000,
                msg: "删除考勤记录失败，记录不存在",
                data: null
            });
        }
    } catch (error) {
        console.error("删除考勤记录失败:", error);
        res.send({
            code: 5000,
            msg: "删除考勤记录失败",
            data: null
        });
    }
});

module.exports = router;